import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import java.util.regex.*;
import java.util.StringTokenizer;

public class FileControl{
    File dbfile;
    File countfile;
    public FileControl(String dbfile, String countfile){
        this.dbfile = new File(dbfile);
        this.countfile = new File(countfile);
    }

    public String getList(String id){
        String text="";
        try{
            File inFile = this.dbfile;
            if (!inFile.isFile()) {
                System.out.println("Parameter is not an existing file");
                //return = flag;
            }
            BufferedReader br = new BufferedReader(new FileReader(inFile));
            String line = null;
            while ((line = br.readLine()) != null) {
                if(getId(line).trim().equals(id)){
                    text=text+line+"\n";
                }
            }
            br.close();

        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return text;

    }

    public String getString(String idx){
        String text="";

        try{
            File inFile = this.dbfile;
            if (!inFile.isFile()) {
                System.out.println("Parameter is not an existing file");
                //return false;
            }
            BufferedReader br = new BufferedReader(new FileReader(inFile));
            String line = null;
            while ((line = br.readLine()) != null) {
                if(getIdx(line).trim().equals(idx)){
                    text=line;
                }
            }
            br.close();

        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return text;
    }


    public boolean statuschange(String idx,String status){
        boolean flag=false;
        try {
            File inFile = this.dbfile;
            if (!inFile.isFile()) {
                System.out.println("Parameter is not an existing file");
                return false;
            }
            File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
            BufferedReader br = new BufferedReader(new FileReader(inFile));
            PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                if(getIdx(line).trim().equals(idx.trim())){
                    line = getIdx(line) + "," + getId(line) + "," + status + "," + getIssue(line);
                    flag = true;
                }
                pw.println(line);
                pw.flush();
            }
            pw.close();
            br.close();

            if (!inFile.delete()) {
                System.out.println("Could not delete file");
                return false;
            }

            if (!tempFile.renameTo(inFile))
                System.out.println("Could not rename file");
        }
        catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        return flag;

    }
    public boolean removeLine(String idx) {
        boolean flag = false;
        try {
            Pattern reg = Pattern.compile(idx + ".*");
            File inFile = dbfile;
            if (!inFile.isFile()) {
                System.out.println("Parameter is not an existing file");
                return false;
            }
            File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
            BufferedReader br = new BufferedReader(new FileReader(dbfile));
            PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
            String line = null;


            while ((line = br.readLine()) != null) {
                Matcher fit = reg.matcher(line);
                if(!getIdx(line).trim().equals(idx.trim()))
                {
                    pw.println(line);
                    pw.flush();
                }else{
                    flag = true;
                }
            }
            pw.close();
            br.close();

            if (!inFile.delete()) {
                System.out.println("Could not delete file");
                return false;
            }

            if (!tempFile.renameTo(inFile))
                System.out.println("Could not rename file");

        }
        catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        return flag;
    }

    public void addLine(String str){
        try{
            File cfd =countfile;
            boolean exists = cfd.exists();
            String line;
            int count=0;
            if(!exists){
                PrintWriter ooout = new PrintWriter(new FileWriter(cfd));
                ooout.println("0");
                ooout.flush();
                ooout.close();
                cfd.createNewFile();
            }else{
                BufferedReader bbr = new BufferedReader(new FileReader(cfd));
                count = Integer.parseInt(bbr.readLine());
                bbr.close();
            }

            str = count + "," + str;
            System.out.println(count);
            count++ ;
            PrintWriter oout = new PrintWriter(new FileWriter(cfd));
            oout.println(Integer.toString(count));
            //oout.println("aa");
            oout.flush();
            oout.close();


            File file = dbfile;
            exists = file.exists();
            if(!exists){
                file.createNewFile();
            }


            PrintWriter out = new PrintWriter(new FileWriter(file,true));
            out.println(str);
            out.flush();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public String getIdx(String str){
        StringTokenizer tokens = new StringTokenizer( str, "," );
        tokens.hasMoreElements(); 
        String idx =  tokens.nextToken();
        return idx;
    }

    public String getId(String str){
        StringTokenizer tokens = new StringTokenizer( str, "," );
        tokens.hasMoreElements(); 
        tokens.nextToken();
        String id =  tokens.nextToken();
        return id;
    }

    public String getStatus(String str){
        StringTokenizer tokens = new StringTokenizer( str, "," );
        tokens.hasMoreElements(); 
        tokens.nextToken();
        tokens.nextToken();
        String Status =  tokens.nextToken();
        return Status;
    }

    public String getIssue(String str){
        StringTokenizer tokens = new StringTokenizer( str, "," );
        tokens.hasMoreElements(); 
        tokens.nextToken();
        tokens.nextToken();
        tokens.nextToken();
        String issue =  tokens.nextToken();
        return issue;
    }

    public static void main(String[] args) {
        FileControl fc = new FileControl("dbfile.txt","count.txt");
        fc.addLine("nan1004au,start,심심해");

        if(fc.statuschange("5","fix")){
            System.out.println("ൂ성공");
        }else{
            System.out.println("ൂ실패");
        }

        if(fc.removeLine("3")){
            System.out.println("ൂ성공");
        }else{
            System.out.println("ൂ실패");
        }

    }
}

